import web
import json
from mimerender import mimerender

import urllib

render_xml = lambda message: '<message>%s</message>'%message
render_json = lambda **args: json.dumps(args)
render_html = lambda message: '<html><body>%s</body></html>'%message
render_txt = lambda message: message

urls = (
    '/(.*)', 'greet'
)


app = web.application(urls,globals())

class greet:
    @mimerender(
        default = 'html',
        html = render_html,
        xml  = render_xml,
        json = render_json,
        txt  = render_txt
    )
    def GET(self, name):
        if not name: 
            name = 'world'
        name = urllib.unquote_plus(name)
        import pickle
        from nltk.tokenize import word_tokenize
        tagger = pickle.load(open("C:/Python26/taggers/treebank_brill_aubt/treebank_brill_aubt.pickle"))
        words = word_tokenize(name)
        tagged = tagger.tag(words)
        return {'message': tagged.__str__() }
    def POST(self, name):
        if not name: 
            name = 'world'
        _name = str(urllib.unquote_plus(name))
        
        import pickle
        from nltk.tokenize import word_tokenize
        tagger = pickle.load(open("C:/Python26/taggers/treebank_brill_aubt/treebank_brill_aubt.pickle"))
        words = word_tokenize(_name)
        tagged = tagger.tag(words)
        return {'message': tagged.__str__() }


if __name__ == "__main__":
    app.run()
